package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.CommentDao;
import com.qf.lanzhou.blog.api.pojo.entity.Comment;
import com.qf.lanzhou.blog.api.pojo.vo.CommentVo;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.annotation.WebServlet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 是柠檬甜
 * @ClassName CommentDaoImpl
 * @description 评论数据库接口实现类
 * @date 2020/12/16 21:29
 * @modifier 是柠檬甜
 * @date 2020-12-16 21:29
 * @Version V1.0
 */
public class CommentDaoImpl implements CommentDao {
    private static QueryRunner queryRunner;
    static {
        queryRunner=new QueryRunner(DruidUtil.getDataSource());
    }
    @Override
    public List select(String userName, Long articleId,String commentStatus) {
        Object[] obj={userName,articleId,commentStatus};
        String sql="select comment_id,comment_content,comment_time,user_name,article_name\n" +
                " FROM t_comment c INNER JOIN t_article a \n" +
                "on c.article_id=a.article_id\n" +
                "WHERE user_name=? or c.article_id=? or comment_status=?;";
        BeanListHandler<List> commentList = BeanProcessor.getBeanListHandler(CommentVo.class);
        List query=null;
        try {
            query= queryRunner.query(sql, commentList, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public Boolean insert(String content,String userName, Long articleId) {
        Object[] obj={content,userName,articleId};
        String sql="INSERT INTO t_comment(comment_content,user_name,article_id) VALUES (?,?,?);";
        int result=0;
        try {
            result = queryRunner.update(sql, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (result==0){
            return false;
        }
        return true;
    }

    @Override
    public Boolean update(String commentStatus) {
        Object[] obj={commentStatus};
        String sql="UPDATE t_comment SET comment_status='已读' WHERE comment_id=?;";
        int result=0;
        try {
            result = queryRunner.update(sql, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (result==0){
            return false;
        }
        return true;
    }
}
